package cn.com.sbell.vss.servlet;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
import java.util.Locale;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.beanutils.BeanUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.NoSuchMessageException;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.CannotGetJdbcConnectionException;
import org.springframework.web.context.support.WebApplicationContextUtils;

import cn.com.sbell.vss.ICommander;
import cn.com.sbell.vss.ILogCommander;
import cn.com.sbell.vss.ILogExCommander;
import cn.com.sbell.vss.IProtectedCommander;
import cn.com.sbell.vss.Response;
import cn.com.sbell.vss.Util;
import cn.com.sbell.vss.VissSession;
import cn.com.sbell.vss.VssConstants;
import cn.com.sbell.vss.VssException;
import cn.com.sbell.vss.ems.dao.ILogDao;
import cn.com.sbell.vss.license.HostIdUtil;
import cn.com.sbell.vss.license.VssLicense;
import cn.com.sbell.vss.license.validator.LicenseValidator;
import cn.com.sbell.vss.util.SessionHolder;
import cn.com.sbell.vss.csg.vap.AHCommand;
import cn.com.sbell.vss.csg.vsclient.VsClientFactory;
//import cn.com.sbell.vss.util.SessionMapper;
import cn.com.sbell.vss.Network;
import cn.com.sbell.vss.csg.vsclient.util.CSGConfiguration;
import cn.com.sbell.vss.csg.vap.util.CSGRelayer;


public class DispatchServlet extends HttpServlet {

	private static final Log log = LogFactory.getLog(DispatchServlet.class);

	private static ApplicationContext ac = null;

	private static VssLicense lic = null;

	private static String errorMessage = null;

	private static final String HOST_ID;

	private static boolean traceEnabled = true;

	private static String USER_SESSION_LOCALE="USER_SESSION_LOCALE_KEY";
	
//	private static ILogDao logDao;

	static {
		log.info("Getting host id ...");
		HOST_ID = HostIdUtil.getLocalHostId();
		log.info("HOST ID = " + HOST_ID);
	}

	// Initialize global variables
	public synchronized void init() throws ServletException {
		System.out.println("DispatchServlet init ...");

		if (ac == null) {
			ac = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
//			try {
//				logDao = (ILogDao) ac.getBean("logDao");
//			} catch (NoSuchBeanDefinitionException e) {
//				log.debug("BeansException(" + e.getMessage()
//						+ ") when get bean: logDao.");
//			} catch (BeansException e) {
//				log.debug("BeansException(" + e.getMessage()
//						+ ") when get bean: logDao.");
//			}
		}
		
		/*注释掉读取license.lic文件的代码
		if (lic == null) {			
			InputStream is = getServletContext().getResourceAsStream("/WEB-INF/license.lic");
			if (is == null) {
				DispatchServlet.errorMessage = "License file NOT found!";
				return;
			}
			File f = new File("_lib_temp");
			if (!f.exists()) {
				f.mkdirs();
			}
			File licf = new File(f, "licence.lic");
			if (licf.exists()) {
				licf.delete();
			}
			BufferedInputStream bis = null;
			FileOutputStream fos = null;
			try {
				bis = new BufferedInputStream(is);
				fos = new FileOutputStream(licf);
				while (bis.available() > 0) {
					byte[] bytes = new byte[1024];
					int i = bis.read(bytes);
					if (i <= 0) {
						break;
					}
					fos.write(bytes, 0, i);
				}
			} catch (IOException ex) {
				DispatchServlet.errorMessage = ex.getLocalizedMessage();
				log.error("IOException", ex);
				return;
			}
			if (bis != null) {
				try {
					bis.close();
				} catch (IOException ex) {
					DispatchServlet.errorMessage = ex.getLocalizedMessage();
					log.error("IOException", ex);
					return;
				}
			}
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException ex) {
					DispatchServlet.errorMessage = ex.getLocalizedMessage();
					log.error("IOException", ex);
					return;
				}
			}
			

			try {
				VssLicense lic1 = LicenseValidator.verifyLicense(licf);
				if (lic1.getHostId().equals(HOST_ID)) {
					lic = lic1;
				} else {
					DispatchServlet.errorMessage = "Host ID NOT matchs!";
					log.error("Host ID [" + lic1.getHostId() + "/" + HOST_ID
							+ "] NOT matchs!");
					return;
				}
			} catch (Exception ex) {
				DispatchServlet.errorMessage = ex.getLocalizedMessage();
				log.error("Exception", ex);
				return;
			} finally {
				if (licf.exists()) {
					licf.delete();
				}
				if (f.exists()) {
					f.delete();
				}
			}
		}
		*/
		
		lic = new VssLicense();
		lic.setHostId("10a2002");
		lic.setMaxUserAmount(100);
		lic.setMaxCameraAmount(10000);
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
		Date expDate = null;
		Date validDate = null;
		try {
			expDate = sdf.parse("01/01/5000");
			validDate = sdf.parse("01/01/2000");
		} catch (ParseException e1) {
			e1.printStackTrace();
		}
		lic.setExpiringDate(expDate);
		lic.setValidDate(validDate);
		
		//enable or disable database log
		//traceEnabled = ((Boolean) ac.getBean("traceEnabled")).booleanValue();
		//traceEnabled=new Boolean(false);
	}	

	// Clean up resources
	public void destroy() {
		log.warn("DispatchServlet destroy ...");
	}

	// Process the HTTP Get request
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		process(request, response, System.currentTimeMillis());
	}

	// Process the HTTP Post request
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		process(request, response, System.currentTimeMillis());
	}

	protected void process(HttpServletRequest request,
			HttpServletResponse response, long beginTime)
			throws ServletException, IOException {
		// set headers
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/xml; charset=UTF-8");
		if (request.getCharacterEncoding() == null) {
			log.debug("CharacterEncoding is null, set to default 'UTF-8'");
			request.setCharacterEncoding("UTF-8");
		}
		if (log.isDebugEnabled()) {
			log.debug("CharacterEncoding: " + request.getCharacterEncoding());
		}		
		
		String tid = request.getHeader(VssConstants.TRANSACTION_ID);
		String addr = request.getHeader(VssConstants.CLIENT_ADDRESS);
		String locale=request.getParameter("locale");
		if (!Util.strIsNull(tid)) {
			response.addHeader(VssConstants.TRANSACTION_ID, tid);
		}
		if (!Util.strIsNull(addr)) {
			response.addHeader(VssConstants.CLIENT_ADDRESS, addr);
		}
		if (Util.strIsNull(locale)) {
			locale = CSGConfiguration.CSG_DEFAULT_LOCALE;
		}
		String[] tempLocale = locale.split("_");
		Locale userLocale = null;
		if (2 == tempLocale.length) {
			userLocale = new Locale(tempLocale[0], tempLocale[1]);
		} else {
			userLocale = new Locale(tempLocale[0]);
		}
		if (log.isDebugEnabled()) {
			log.debug("User Session Locale is:" + userLocale);
		}
		
		request.setAttribute(USER_SESSION_LOCALE, userLocale);
		
		if (lic == null) {
			Response res = Response.createResponse(request);
			res.setReturnCode(VssConstants.CODE_INVALID_LICENSE);
			String returnMessage = getMessage("code." + res.getReturnCode(),
					new Object[] { errorMessage, HOST_ID },request);
			res.setReturnMessage(returnMessage);
			sendResponse(request, response, res, System.currentTimeMillis());
			return;
		} else {
			request.setAttribute(VssConstants.KEY_VSS_LICENSE, lic);
		}
		request.setAttribute(VssConstants.KEY_VSS_LICENSE, lic);

		if (log.isDebugEnabled()) {
			Enumeration en = request.getHeaderNames();
			if (log.isDebugEnabled()) {
				log.debug("\n\n##############################################");
			}
			if (log.isInfoEnabled()) {
				log.info("Request URI: " + request.getRequestURI() + ", from "
						+ request.getRemoteAddr());
			}
			if (log.isDebugEnabled()) {
				log.debug("Context Path: " + request.getContextPath());
				log.debug("Request URL: " + request.getRequestURL().toString());
				log.debug("Query String: " + request.getQueryString());
				log.debug("================ HTTP Header ================");
				while (en.hasMoreElements()) {
					String name = (String) en.nextElement();
					Enumeration value = request.getHeaders(name);
					while (value.hasMoreElements()) {
						log.debug(name + " = " + value.nextElement());
					}
				}
				log.debug("============================================");
			}
		}
		
		// start process
		String cp = request.getContextPath();
		String command = request.getRequestURI();
		if (command.length() >= cp.length()) {
			command = command.substring(cp.length());
			if (command.startsWith("/")) {
				command = command.substring(1);
			}
		}
		if (log.isDebugEnabled()) {
			log.debug("###  >>> VAP COMMAND = " + command);
			log.debug("================ HTTP Parameter ================");
			Enumeration en = request.getParameterNames();
			while (en.hasMoreElements()) {
				String key = (String) en.nextElement();
				String[] values = request.getParameterValues(key);
				for (int i = 0; i < values.length; i++) {
					log.debug(key + " = " + values[i]);
				}
			}
			log.debug("================================================");
		}

		if ("VissDebug".equals(command)) {
			doDebug(request, response);
			return;
		}

		try {
			// executing command
			ICommander commander = null;
			try {
				commander = (ICommander) ac.getBean(command);
			} catch (NoSuchBeanDefinitionException e) {
				log.debug("NoSuchBeanDefinitionException: " + command);
				throw new VssException(
						VssConstants.CODE_UNSUPPORTED_COMMAND_ERROR);
			}

			if (commander == null) {
				throw new VssException(
						VssConstants.CODE_UNSUPPORTED_COMMAND_ERROR);
			}

			request.setAttribute(VssConstants.KEY_COMMAND, command);
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//for distinguish AH and VS.
//
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
			String vendor=Util.getTrimParameter(request, VssConstants.VENDOR);
			String front_type;
			if(commander instanceof AHCommand)
			{
				front_type=VssConstants.FRONT_TYPE_AH;
			}else if(vendor.equalsIgnoreCase(VsClientFactory.VENDOR_IPCAMERA)){
				front_type=VssConstants.FRONT_TYPE_IPCAM;
			}else{
				front_type=VssConstants.FRONT_TYPE_VS;
			}
			request.setAttribute(VssConstants.KEY_FRONT_TYPE, front_type);

			// SET SESSION DATA TO REQUEST ATTRIBUTE
			SessionHolder sessionHolder = (SessionHolder) ac
					.getBean("sessionHolder");
			String sessionId = Util.getSessionId(request);
			VissSession sessionData = null;
			if (log.isDebugEnabled())
				log.debug("SessionId is: " + sessionId);
			if (sessionId != null) {
				sessionData = sessionHolder.getSession(sessionId);
				if (sessionData != null) {
					request.setAttribute(VssConstants.KEY_USER_SESSION_DATA,
							sessionData);
					if (log.isDebugEnabled()) {
						log.debug("User session data is: " + sessionData);
					}
				} else {
					log.debug("User session data NOT found!");
				}
			}

			if ((sessionData == null)
					&& (commander instanceof IProtectedCommander)) {
				doLogin(request);
			}
//added vondart
//			if (traceEnabled && (commander instanceof ILogCommander)) {
//				cn.com.sbell.vss.ems.entity.Log vssLog = new cn.com.sbell.vss.ems.entity.Log();
//				vssLog.setLogDate(new Date(System.currentTimeMillis()));
//				request.setAttribute(VssConstants.KEY_VSS_LOG, vssLog);
//			}
			
			
			

			Response res =null;
//			do routing
			 String routeUrl=null;
				try {
					routeUrl=Util.getRoute(request);
				} catch (VssException e) {
					// TODO: handle exception
					log.debug("Get route error:"+e.getResultCode()+",message:"+Arrays.toString(e.getFields()));
					res= CSGRelayer.generateResponse(request,e.getResultCode(),e.getFields());
					res.setCommand(command);
					sendResponse(request, response, res, beginTime);
					return;
				}
			
//			check relay
if(null!=routeUrl){
//	relay
				res=CSGRelayer.relayRequest(request, routeUrl,command);
				
}else{
//			normal processing
			res= commander.execute(request);
}
			// check whether really need log 
//			added vondart
//			if (traceEnabled && (commander instanceof ILogExCommander)
//					&& !((ILogExCommander) commander).isNeedLog()) {
//				request.removeAttribute(VssConstants.KEY_VSS_LOG);
//			}
			// return response ...
			res.setCommand(command);
			if((null==res.getReturnMessage())||(res.getReturnMessage().trim().length()==0)){
			res.setReturnMessage(getMessage("code." + res.getReturnCode(),request));
			}
			sendResponse(request, response, res, beginTime);
		} catch (VssException e) {
			if (log.isDebugEnabled()) {
				log.debug("Command=" + command + ", resultCode = "
						+ e.getResultCode(),e);
			}
			// return error response
			Response res = Response.createResponse(request);
			if (e.getResultCode() == null || "".equals(e.getResultCode())) {
				processUnknownError(request, response, command, null, beginTime);
				return;
			}
			res.setCommand(command);
			res.setReturnCode(e.getResultCode());
			if(VssConstants.CODE_INVALIDE_ROUTE.equalsIgnoreCase(e.getResultCode()))
			{
				
				res.setReturnMessage(e.getFields()[0]);
				
			}else if (e.getFields() != null) {
				Object[] fields = new Object[e.getFields().length];
				for (int i = 0; i < fields.length; i++) {
					fields[i] = getMessage(e.getFields()[i],request);
				}
				String returnMessage = getMessage("code." + e.getResultCode(),
						fields,request);
				res.setReturnMessage(returnMessage);

			} else {
				String returnMessage = getMessage("code." + e.getResultCode(),request);
				res.setReturnMessage(returnMessage);
			}

			sendResponse(request, response, res, beginTime);
		} catch (CannotGetJdbcConnectionException ex) {
			log.error("CannotGetJdbcConnectionException", ex);
			Response res = Response.createResponse(request);
			res.setReturnCode(VssConstants.CODE_CANNOT_GET_JDBC_CONNECTION);
			String returnMessage = getMessage("code."
					+ VssConstants.CODE_CANNOT_GET_JDBC_CONNECTION,request);
			res.setReturnMessage(returnMessage);
			sendResponse(request, response, res, beginTime);
		} catch (DataAccessException ex) {
			log.warn("DataAccessException", ex);
			Response res = Response.createResponse(request);
			res.setReturnCode(VssConstants.CODE_DATABASE_ACCESS_ERROR);
			String returnMessage = getMessage("code."
					+ VssConstants.CODE_DATABASE_ACCESS_ERROR,request);
			res.setReturnMessage(returnMessage);
			sendResponse(request, response, res, beginTime);
		} catch (Throwable ex) {
			log.warn("Unknown Error", ex);
			processUnknownError(request, response, command, ex, beginTime);
		}
		
	}
	
	
	private void doLogin(HttpServletRequest request) {
		ICommander userLogin = null;
		try {
			userLogin = (ICommander) ac.getBean("UserLogin");
		} catch (NoSuchBeanDefinitionException e) {
			log.debug("NoSuchBeanDefinitionException: UserLogin");
			throw new VssException(VssConstants.CODE_UNSUPPORTED_COMMAND_ERROR);
		}
		try {
			Response res = userLogin.execute(request);
			if (res == null
					|| !VssConstants.CODE_SUCCESS.equals(res.getReturnCode())) {
				
//				modified vondart
				if(!VssConstants.CODE_INVALIDE_ROUTE.equals(res.getReturnCode())){
					log.warn("Login failed.");
				throw new VssException(VssConstants.CODE_NEED_LOGIN);
				}else{
					log.warn("Invalid route");
					throw new VssException(VssConstants.CODE_INVALIDE_ROUTE,res.getReturnMessage());
				}
			}
		} catch (VssException ex) {
//			added vondart
			if(VssConstants.CODE_INVALIDE_ROUTE.equals(ex.getResultCode()))
			{
				log.warn("Invalid route");
				throw new VssException(VssConstants.CODE_INVALIDE_ROUTE,ex.getFields()); 
			}
			log.warn("Login failed.");
			throw new VssException(VssConstants.CODE_NEED_LOGIN);
		}
	}

	private void doDebug(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String name = request.getParameter("bean");
		Object bean = ac.getBean(name);
		PrintWriter writer = response.getWriter();
		writer.print("<bean name=\"" + name + "\"><![CDATA[ ");

		// XStream xs = new XStream();
		// xs.toXML(bean,writer);
		String s = ToStringBuilder.reflectionToString(bean,
				ToStringStyle.MULTI_LINE_STYLE, true);
		writer.println(s);
		Field[] fs = bean.getClass().getDeclaredFields();
		if (fs != null && fs.length > 0) {
			writer.println("");
			writer.println("=== STATIC FIELDS  === ");
			writer.println("");
			for (int i = 0; i < fs.length; i++) {

				if ((fs[i].getModifiers() & Modifier.STATIC) == Modifier.STATIC) {
					writer.print(fs[i].getName());
					writer.print(" = ");
					try {
						boolean isAccessible = fs[i].isAccessible();
						if (!isAccessible) {
							fs[i].setAccessible(true);
						}
						String ss = ToStringBuilder.reflectionToString(fs[i]
								.get(bean), ToStringStyle.MULTI_LINE_STYLE,
								true);
						writer.println(ss);
						if (!isAccessible) {
							fs[i].setAccessible(false);
						}
					} catch (IllegalArgumentException ex) {
						log.warn("IllegalArgumentException", ex);
					} catch (IllegalAccessException ex) {
						log.warn("IllegalAccessException", ex);
					}
					writer.print(" \n ");
				}

			}
		}
		writer.print("]]> </bean>");
		writer.flush();
	}

	public static String getMessage(String key, Object[] args,HttpServletRequest request) {
		try {
			return ac.getMessage(key, args, (Locale)request.getAttribute(USER_SESSION_LOCALE));
		} catch (NoSuchMessageException e) {
			log.warn("Message of key " + key + " NOT found!");
			return key;
		}
	}

	public static String getMessage(String key,HttpServletRequest request) {
		try {
			return ac.getMessage(key, null, (Locale)request.getAttribute(USER_SESSION_LOCALE));
		} catch (NoSuchMessageException e) {
			log.warn("Message of key " + key + " NOT found!");
			return key;
		}
	}

	private static void processUnknownError(HttpServletRequest request,
			HttpServletResponse response, String command, Throwable e,
			long beginTime) throws IOException {
		Response res = Response.createResponse(request);
		res.setReturnCode(VssConstants.CODE_UNKNOWN_ERROR);
		res.setReturnMessage(getMessage("code." + res.getReturnCode(),request) + ": "
				+ e.getLocalizedMessage());
		res.setCommand(command);
		sendResponse(request, response, res, beginTime);
	}

	private static void sendResponse(HttpServletRequest httpRequest,
			HttpServletResponse httpResponse, Response response, long beginTime)
			throws IOException {

		VissSession usd = (VissSession) httpRequest
				.getAttribute(VssConstants.KEY_USER_SESSION_DATA);
		if (usd != null) {
			Cookie cookie = new Cookie(VssConstants.SESSION_ID, usd
					.getSessionId());
			// cookie.setMaxAge(600000);
			httpResponse.addCookie(cookie);
		}
		if (log.isInfoEnabled()) {
			log.info("Response command: " + response.getCommand()
					+ ", return code: " + response.getReturnCode()+", Time takes: "+(1.0 * (System.currentTimeMillis() - beginTime) / 1000L));
		}
		String xmlLine = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
		String timeComment = "<!-- Time takes: "
				+ (1.0 * (System.currentTimeMillis() - beginTime) / 1000L)
				+ " seconds -->";
		String xml = null;

		Object parameters = response.getParameters();
		if (parameters instanceof String) {
			Element res = DocumentHelper.createElement("response");
			res.addAttribute("command", response.getCommand());
			res.add(DocumentHelper.createElement("result").addAttribute("code",
					response.getReturnCode()).addText(
					response.getReturnMessage()));

			try {
				Document doc = DocumentHelper.parseText((String) parameters);
				res.add(doc.getRootElement());
			} catch (DocumentException ex) {
				log.warn("DocumentException", ex);
				res.addElement("parameters");
			}
			xml = res.asXML();

		} else if (parameters instanceof Element) {
			Element res = DocumentHelper.createElement("response");
			res.addAttribute("command", response.getCommand());
			res.add(DocumentHelper.createElement("result").addAttribute("code",
					response.getReturnCode()).addText(
					response.getReturnMessage()));
			res.add(((Element) response.getParameters()).detach());

			xml = res.asXML();
		} else {
			StringBuffer sb = new StringBuffer();
			String result = DocumentHelper.createElement("result")
					.addAttribute("code", response.getReturnCode()).addText(
							response.getReturnMessage()).asXML();
			sb.append("<response command=\"").append(response.getCommand())
					.append("\">");
			sb.append(result);
			if (response.getParameters() == null) {
				sb.append("<parameters/>");
			} else {
				sb.append(Util.toXmlString(response.getParameters(),
						"parameters"));
			}
			sb.append("</response>");
			xml = sb.toString();
		}

		int contentLength = xml.getBytes(httpResponse.getCharacterEncoding()).length
				+ timeComment.getBytes(httpResponse.getCharacterEncoding()).length
				+ xmlLine.getBytes(httpResponse.getCharacterEncoding()).length;

		httpResponse.setContentLength(contentLength);

		PrintWriter out = httpResponse.getWriter();
		out.print(xmlLine);
		out.print(timeComment);
		out.print(xml);
		out.flush();
		if (log.isDebugEnabled()) {
			log.debug("Content Length = " + contentLength+"\n"+xml);
		}
//		added vondart
//		if (traceEnabled && logDao != null) {
//			writeLog(httpRequest, response, usd);
//		}
	}
//	added vondart
//	private static void writeLog(HttpServletRequest httpRequest,
//			Response response, VissSession usd) {
//		// insert log to database
//		cn.com.sbell.vss.ems.entity.Log vssLog = (cn.com.sbell.vss.ems.entity.Log) httpRequest
//				.getAttribute(VssConstants.KEY_VSS_LOG);
//		if (vssLog != null) {
//			if (usd != null) {
//				vssLog.setUserId(usd.getUserId());
//			}
//			vssLog.setCommand(response.getCommand());
//			StringBuffer sb = new StringBuffer();
//			Enumeration en = httpRequest.getParameterNames();
//			while (en.hasMoreElements()) {
//				String key = (String) en.nextElement();
//				String[] values = httpRequest.getParameterValues(key);
//				for (int i = 0; i < values.length; i++) {
//					sb.append(key).append("=").append(values[i]).append("&");
//				}
//			}
//			// Enumeration enu = httpRequest.getParameterNames();
//			// while (enu.hasMoreElements()) {
//			// String key = (String) enu.nextElement();
//			// String value = httpRequest.getParameter(key);
//			// sb.append(key).append("=").append(value).append("&");
//			// }
//			vssLog.setInputParameters(sb.toString());
//			if (VssConstants.CODE_SUCCESS.equals(response.getReturnCode())) {
//				vssLog.setLogLevel(0);
//			} else {
//				vssLog.setLogLevel(1);
//			}
//			vssLog.setResultCode(response.getReturnCode());
//			vssLog.setResultMessage(response.getReturnMessage());
//
//			try {
//				logDao.insert(vssLog);
//			} catch (Throwable ex) {
//				log.warn("Write log failed.", ex);
//			}
//		}
//	}

}